package locations.dao.implement;

import java.util.List;
import locations.model.Usuario;

public class UserDAOHibernateJPA extends GenericDAOHibernateJPA<Usuario>
{

    public UserDAOHibernateJPA()
    {
            super(Usuario.class);
    }

    public boolean exists(String usuario, long id)
    {
        boolean started = this.getTransaction().isActive();
        if (!started)
        {
            this.getTransaction().begin();            
        }
        String queryId = (id == 0 ? "" :  " and u.id !='" + id + "'");
        List result= entityManager.createQuery
                ("from Usuario u where u.usuario='" + usuario + "'" + queryId).getResultList();
//            this.getTransaction().commit();            
        return ! result.isEmpty();
    }

    public List<Usuario> getAllValid()
    {
        boolean started = this.getTransaction().isActive();
        if (!started)
        {
            this.getTransaction().begin();            
        }
        List result= entityManager.createQuery
                ("from Usuario u where u.valido=true").getResultList();
//            this.getTransaction().commit();            
        return result;
    }
    
    public boolean existUser(String usuario, String pass)
    {
        boolean started = this.getTransaction().isActive();
        if (!started)
        {
            this.getTransaction().begin();            
        }
        List result= entityManager.createQuery
                ("from Usuario u where u.usuario='" + usuario + "' and u.contrasena='" + pass + "'").getResultList();
//            this.getTransaction().commit();            
        return ! result.isEmpty();
    }

}
